package org.progin.cloudfolder;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Util {

        private static final int BUFF_SIZE = 1024 * 64;
	private static byte[] buff = new byte[BUFF_SIZE]; // 64KB buffer
	public static void StreamCopyer(InputStream in, OutputStream out, long length) {
		long total = 0;
		int len = 0;
                while(true) {
                    try {
                        int maxfetch = length - total >= BUFF_SIZE ? BUFF_SIZE : (int)(length-total);
                        while(total < length && (len = in.read(buff, 0, maxfetch)) >= 0) {
                            out.write(buff, 0, len);
                            total += len;
                            maxfetch = length - total >= BUFF_SIZE ? BUFF_SIZE : (int)(length-total);
                        }
                        if(length < total) {
                            System.out.println("Extra!!! " + length + " : " + total);
                        } else {
                            System.out.println("Cool!!! " + length + " : " + total);
                        }
                        break;
                    } catch (IOException ex) {
                        System.out.println("error-streamcopyer");
                    }
                }
	}
	
	public static void StreamCopyer(InputStream in, OutputStream out) {
		int len = 0;
                while(true) {
                    try {
                        while((len = in.read(buff)) >= 0) {
                            out.write(buff, 0, len);
                        }
                        break;
                    } catch (IOException ex) {
                        System.out.println("error-streamcopyer");
                    }
                }
	}
}
